package jiubeirobot.com.robotshop.utils;

import com.alibaba.fastjson.JSON;

import java.util.HashMap;
import java.util.Map;

/**
 * 类描述    :
 * 创建人    : TheJoker丶豪  on 2017/9/20.
 * 修改人    :
 * 修改时间  :
 * 修改备注  :
 */
public class Text {

    public static void main(String[] args) {
        Map<String, Object> map = new HashMap<>();
        map.put("address", "科兴科学园1A");
        map.put("machine_id", "1c:a1:d6:c1:d7:43");
        map.put("area_id", 4524161);
        map.put("account", "13537859653");
        map.put("coordinate", "112.23,23.22");
        String json = JSON.toJSONString(map);
        String result = Text.encrypt(json);
        String signature = result.substring(0, 32);
        String message = result.substring(32);
        System.out.println("signature:" + signature);
        System.out.println("message:" + message);
        String source = Text.decrypt(signature + message).trim();
        System.out.println(source);
    }

    public static String encrypt(String source) {
        String key = AES.generateRandomString();
        String iv = AES.generateRandomString();
        String message = AES.encrypt(source, key, iv);
        StringBuilder builder = new StringBuilder();
        builder.append(key).append(iv).append(message);
        return builder.toString();
    }

    public static String decrypt(String dest) {
        String key = dest.substring(0, 16);
        String iv = dest.substring(16, 32);
        String source = AES.decrypt(dest.substring(32), key, iv);
        return source;
    }
}
